package com.ioPackage;

import java.io.*;

/**
 * @ClassName ObIOTest
 * @Description 对象序列化、反序列化
 * @Author 2214361854
 * @Date 2022/3/1 13:31
 * @Version 1.0
 */
public class ObIOTest {
    public static void main(String[] args) throws Exception {
        Student s = new Student("小王","123456");
        System.out.println(s);
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("src\\text03.txt"));
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("src\\text03.txt"));
        //序列化对象
        oos.writeObject(s);
        oos.close();
        //反序列化
        Student s2 = (Student)ois.readObject();
        System.out.println(s2.toString());

    }
}

/**
 * 序列化对象，必须实现 Serializable 接口
 * 成员变量使用 transient 修饰就不会被序列化
 */
class Student implements Serializable{
    /**
     * 申请序列化版本号，序列化与反序列化版本号必须一致才可以
     */
    private static final long serialVersionUID = 1;
    private String name;
    private transient String passWrd;

    public Student(String name, String passWrd) {
        this.name = name;
        this.passWrd = passWrd;
    }

    public Student() {
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", passWrd='" + passWrd + '\'' +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassWrd() {
        return passWrd;
    }

    public void setPassWrd(String passWrd) {
        this.passWrd = passWrd;
    }
}